#include "Utils.h"
#include <log/log.h>

namespace vendor {
namespace ecarx {
namespace xma {
namespace iplm {
namespace V1_0 {
namespace implementation {

void convertToBigEndian(iplm_activity_t * packet) {
    uint8_t * p = (uint8_t *) &packet->header;

    uint16_t service_id       = packet->header.service_id;
    uint16_t operation_id     = packet->header.operation_id;
    uint32_t length           = packet->header.length;
    uint32_t sender_handle_id = packet->header.senderHandleId;
    uint8_t  pro_version      = packet->header.protocol_version;
    uint8_t  oper_type        = packet->header.op_type;
    uint8_t  payload_type     = packet->header.payload_type;
    uint8_t  proc_field       = packet->header.proc;

    UINT16_TO_BE_STREAM(p, service_id);
    UINT16_TO_BE_STREAM(p, operation_id);
    UINT32_TO_BE_STREAM(p, length);
    UINT32_TO_BE_STREAM(p, sender_handle_id);
    UINT8_TO_BE_STREAM(p, pro_version);
    UINT8_TO_BE_STREAM(p, oper_type);
    UINT8_TO_BE_STREAM(p, payload_type);
    UINT8_TO_BE_STREAM(p, proc_field);
}

void convertToLittleEndian(iplm_activity_t * packet) {
    uint8_t *   unp = (uint8_t *)packet;
    uint16_t    sid;
    uint16_t    op_id;
    uint32_t    len;
    uint32_t    sender_handle_id;
    uint8_t     version;
    uint8_t     op_tp;
    uint8_t     payload_tp;
    uint8_t     proc_flag;

    BE_STREAM_TO_UINT16(sid, unp);
    BE_STREAM_TO_UINT16(op_id, unp);
    BE_STREAM_TO_UINT32(len, unp);
    BE_STREAM_TO_UINT32(sender_handle_id, unp);
    BE_STREAM_TO_UINT8(version, unp);
    BE_STREAM_TO_UINT8(op_tp, unp);
    BE_STREAM_TO_UINT8(payload_tp, unp);
    BE_STREAM_TO_UINT8(proc_flag, unp);

    packet->header.service_id = sid;
    packet->header.operation_id = op_id;
    packet->header.length = len;
    packet->header.senderHandleId = sender_handle_id;
    packet->header.protocol_version = version;
    packet->header.op_type = op_tp;
    packet->header.payload_type = payload_tp;
    packet->header.proc = proc_flag;

    ALOGI("%s, converted Little Endian is [sid %#x], [op_id %#x], [len %#x], [sendHandlerId %#x], [op_type %#x], [payload_type %#x]",
            __func__,
            packet->header.service_id,
            packet->header.operation_id,
            packet->header.length,
            packet->header.senderHandleId,
            packet->header.op_type,
            packet->header.payload_type);
}

} // namespace implementation
} // namespace V1_0
} // namespace iplm
} // namespace xma
} // namespace ecarx
} // namespace vendor